#ifndef ICON_DESIGN_SPECS_H
#define ICON_DESIGN_SPECS_H

#include <QColor>
#include <QPainter>
#include <QPixmap>
#include <QIcon>
#include <QFont>
#include <QFontMetrics>
#include <QRectF>
#include <QPainterPath>
#include <cmath>

/**
 * @brief 图标设计规范 - 统一的设计系统
 *
 * 基于现代设计原则的严格统一规范：
 * - 线条粗细统一规范
 * - 圆角半径完全一致
 * - 间距系统基于4px网格
 * - 色彩体系层级变化
 * - 几何比例黄金分割
 * - 字体规范统一大小
 */
class IconDesignSpecs {
public:
    // 基础尺寸规范
    static constexpr int BASE_PIXMAP_SIZE = 64;        // 基础绘制尺寸（高清源）
    static constexpr int SMALL_SIZE = 16;              // 小尺寸显示
    static constexpr int MEDIUM_SIZE = 24;             // 中等尺寸显示
    static constexpr int LARGE_SIZE = 32;              // 大尺寸显示
    static constexpr int XLARGE_SIZE = 48;             // 超大尺寸显示

    // 线条规范
    static constexpr qreal MAIN_STROKE_WIDTH = 2.5;    // 主线条宽度
    static constexpr qreal DETAIL_STROKE_WIDTH = 2.0;  // 细节线条宽度
    static constexpr qreal FINE_STROKE_WIDTH = 1.5;    // 精细线条宽度

    // 圆角规范
    static constexpr qreal CORNER_RADIUS = 3.0;        // 统一圆角半径
    static constexpr qreal SMALL_CORNER_RADIUS = 2.0;  // 小圆角半径

    // 间距系统 - 基于4px网格
    static constexpr int GRID_UNIT = 4;                // 基础网格单位
    static constexpr int MARGIN = 8;                   // 统一边距
    static constexpr int INNER_SPACING = 6;           // 内部间距
    static constexpr int ELEMENT_SPACING = 8;         // 元素间距

    // 字体规范
    static constexpr int FONT_SIZE_SMALL = 8;         // 小字体
    static constexpr int FONT_SIZE_MEDIUM = 10;       // 中等字体
    static constexpr int FONT_SIZE_LARGE = 12;        // 大字体
    static constexpr int FONT_WEIGHT = QFont::Bold;   // 统一字重

    // 箭头规范
    static constexpr int ARROW_LENGTH = 6;            // 箭头长度
    static constexpr int ARROW_WIDTH = 4;             // 箭头宽度
    static constexpr int ARHEAD_SIZE = 3;             // 箭头头部大小

    // 颜色规范
    static QColor getPrimaryColor(const QColor& themeColor) { return themeColor; }
    static QColor getSecondaryColor(const QColor& themeColor) { return themeColor.lighter(110); }
    static QColor getTertiaryColor(const QColor& themeColor) { return themeColor.darker(115); }
    static QColor getDetailColor(const QColor& themeColor) { return themeColor.lighter(105); }

    // 几何计算工具
    static QRectF getCenteredRect(qreal containerSize, qreal elementSize) {
        qreal margin = (containerSize - elementSize) / 2.0;
        return QRectF(margin, margin, elementSize, elementSize);
    }

    static QPointF getCenterPoint(const QRectF& rect) {
        return rect.center();
    }

    static qreal getGoldenRatioSmall(qreal size) { return size * 0.618; }  // 黄金分割小值
    static qreal getGoldenRatioLarge(qreal size) { return size * 1.618; }  // 黄金分割大值

    // 工具方法
    static void setupPainter(QPainter& painter, const QColor& color, qreal strokeWidth) {
        painter.setPen(QPen(color, strokeWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
        painter.setBrush(Qt::NoBrush);
        painter.setRenderHint(QPainter::Antialiasing, true);
        painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
    }

    static QFont getUnifiedFont(int pixelSize, int weight = FONT_WEIGHT) {
        QFont font;
        font.setPixelSize(pixelSize);
        font.setWeight(static_cast<QFont::Weight>(weight));
        font.setLetterSpacing(QFont::AbsoluteSpacing, -0.2);
        return font;
    }

    // 标准箭头绘制
    static void drawArrow(QPainter& painter, const QPointF& start, const QPointF& end,
                         qreal headSize = ARHEAD_SIZE) {
        painter.drawLine(start, end);

        // 计算箭头方向
        QPointF direction = end - start;
        qreal length = std::sqrt(direction.x() * direction.x() + direction.y() * direction.y());
        if (length < 0.001) return;

        direction /= length;  // 标准化
        QPointF perpendicular(-direction.y(), direction.x());

        // 绘制箭头头部
        QPointF leftHead = end - direction * headSize + perpendicular * headSize * 0.6;
        QPointF rightHead = end - direction * headSize - perpendicular * headSize * 0.6;

        painter.drawLine(end, leftHead);
        painter.drawLine(end, rightHead);
    }

    // 圆形箭头绘制
    static void drawCurvedArrow(QPainter& painter, const QPointF& center, qreal radius,
                               qreal startAngle, qreal spanAngle, qreal headSize = ARHEAD_SIZE) {
        // 绘制弧形
        QRectF arcRect(center.x() - radius, center.y() - radius, radius * 2, radius * 2);
        painter.drawArc(arcRect, static_cast<int>(startAngle * 16), static_cast<int>(spanAngle * 16));

        // 计算箭头头部位置
        qreal endAngle = startAngle + spanAngle;
        QPointF arrowEnd(center.x() + radius * std::cos(endAngle * M_PI / 180),
                        center.y() + radius * std::sin(endAngle * M_PI / 180));

        // 计算箭头方向（切线方向）
        qreal tangentAngle = endAngle + 90;
        QPointF tangentDir(std::cos(tangentAngle * M_PI / 180), std::sin(tangentAngle * M_PI / 180));
        QPointF perpendicular(-tangentDir.y(), tangentDir.x());

        // 绘制箭头头部
        QPointF leftHead = arrowEnd + tangentDir * headSize * 0.8 + perpendicular * headSize * 0.6;
        QPointF rightHead = arrowEnd + tangentDir * headSize * 0.8 - perpendicular * headSize * 0.6;

        painter.drawLine(arrowEnd, leftHead);
        painter.drawLine(arrowEnd, rightHead);
    }
};

#endif // ICON_DESIGN_SPECS_H